"use strict";

/**
 * @ngdoc function
 * @name jgappApp.controller:ProjectFarmerpayCtrl
 * @description
 * # ProjectFarmerpayCtrl
 * Controller of the jgappApp
 */
angular
  .module("jgappApp")
  .controller("ProjectFarmerpayCtrl", function (
    $rootScope,
    $scope,
    openApi,
    Util,
    $location,
    $routeParams,
    FormSubmit,
    localStorageService
  ) {
    var id = ($scope.process_id = $routeParams.id);
    // 选择款项来源
    $scope.sourceTypeList = [
      {
        key: "农民工工资账户余额",
        value: 1
      },
      {
        key: "项目账户余额",
        value: 2
      }
    ];
    $scope.inContractList = [
      {
        key: "否",
        value: 0
      },
      {
        key: "是",
        value: 1
      }
    ];
    // 选择合同类别
    $scope.contractTypeList = [
      {
        key: "分包合同",
        value: "Xm_ContractSubpackage"
      },
      {
        key: "其他合同",
        value: "Xm_ContractOther"
      }
    ];
    var currentYear = (new Date()).getFullYear()
    var prevYear = currentYear - 1
    var prev2Year = currentYear - 2
    $scope.years = [{
      key: currentYear + "年",
      value: currentYear + "年"
    }, {
      key: prevYear + "年",
      value: prevYear + "年"
    }, {
      key: prev2Year + "年",
      value: prev2Year + "年"
    }]
    $scope.months = [
      {
        key: "1月",
        value: "1月"
      },
      {
        key: "2月",
        value: "2月"
      },
      {
        key: "3月",
        value: "3月"
      },
      {
        key: "4月",
        value: "4月"
      },
      {
        key: "5月",
        value: "5月"
      },
      {
        key: "6月",
        value: "6月"
      },
      {
        key: "7月",
        value: "7月"
      },
      {
        key: "8月",
        value: "8月"
      },
      {
        key: "9月",
        value: "9月"
      },
      {
        key: "10月",
        value: "10月"
      },
      {
        key: "11月",
        value: "11月"
      },
      {
        key: "12月",
        value: "12月"
      },
    ]
    $scope.chooseContract = function () {
      var params = $scope.param;
      if (!params.projectId || !params.contractType) {
        Util.Toast("请先选择项目与合同类型");
        return;
      }
      var projectParams = {
        formKey: params.contractType,
        fieldName: "contractNo",
        projectId: params.projectId
      };

      // 清掉关联的编号
      Util.savaParamForReview(params, id);
      localStorageService.set("projectParams", projectParams);
      $location.path("/project/choosedata");
    };

    $scope.chooseProject = function () {
      // 将列表的数据序列化存入缓存以便回来时候使用
      Util.savaParamForReview($scope.param, id);
      $location.path("project/chooseproject");
    };
    $scope.showCertificate = function () {
      $scope.showCerPicker = true;
      $rootScope.$emit("showmasker");
  };
  $scope.deletedItem = function (index) {
      $scope.param.payments.splice(index, 1);
      $scope.costindex = -1;
  };

  $scope.addCertificate = function () {
      $scope.picker = $scope.picker || {};
      var check = false;
      check = checkPicker($scope.picker);
      if (!check) {
          return;
      }
      $scope.param.payments.push($scope.picker);
      // 隐藏表单
      closePicker();
  };
  var closePicker = function () {
      $scope.picker = {};
      $rootScope.$emit("closemasker");
      $scope.showCerPicker = false;
  };
  $scope.$on("closemasker", function () {
      closePicker();
  });
  $scope.closePicker = function () {
      closePicker();
  };
  $scope.swipe = function (index) {
      $scope.costindex = index;
  };
  $scope.resetlist = function () {
      $scope.costindex = -1;
  };
  // 验证是否输入完整
  var checkPicker = function (data) {
      if (!data.sourceType) {
          Util.Toast("请选择款项来源");
          return;
      }
      if (typeof data.inContract ==="undefined") {
          Util.Toast("请选择计入合同已付款");
          return;
      }
      if (!data.amount) {
          Util.Toast("请填写付款金额");
          return;
      }
      if (!data.receivePerson) {
          Util.Toast("请填写收款人");
          return;
      }
      if ( !data.remark) {
          Util.Toast("请填写付款事由");
          return;
      }
      return true;
  };
    $scope.submit = function (operationId) {
      // 底部类型end
      var options = {};
      // 流程id
      options.id = id;
      // 根据operationId判断是草稿还是提交
      options.operationId = operationId;
      options.urlParam = openApi.migrantWages;
      options.param = angular.copy($scope.param, {});
      options.param.month = $scope.param.monthArr.toString();
      
      options.param.paymentDetail = JSON.stringify(options.param.payments);
      delete options.param.payments

      FormSubmit.submit(options, $scope);
    };
    $scope.validate = function () {
      if (!$scope.param.projectName) {
        Util.Toast("请选择项目名称");
        return;
      }
      if (!$scope.param.sourceType) {
        Util.Toast("请选择款项来源");
        return;
      }
      if (!$scope.param.year) {
        Util.Toast("请选择工资年份");
        return;
      }
      if (!($scope.param.monthArr && $scope.param.monthArr.length > 0)) {
        Util.Toast("请选择工资月份");
        return;
      }
      if ($scope.param.contractType && !$scope.param.contractNo) {
        Util.Toast("请选择合同编号");
        return;
      }
      if (!$scope.param.amount) {
        Util.Toast("请填写付款金额");
        return;
      }
      if (!$scope.param.receivePerson) {
        Util.Toast("请填写收款人");
        return;
      }
      if (!$scope.validation.passvalidation) {
        Util.Toast($scope.validation.tip);
        return;
      }
      return true;
    };
    var init = function () {
      // 获取回显的详细信息
      Util.getDetailData(id, function (data) {
        $scope.param = data;
        // 获取项目信息
        var data = localStorageService.get("projectinfo");
        localStorageService.remove("projectinfo");
        var projectreldata = localStorageService.get("projectreldata");
        localStorageService.remove("projectreldata");
        $scope.param = Object.assign({}, $scope.param, data, projectreldata);
        $scope.param.monthArr = $scope.param.monthArr ? $scope.param.monthArr : ($scope.param.month && $scope.param.month.split(",") || [])
        $scope.param.payments = $scope.param.payments || []
      });
    };
    // 初始化
    init();
  });
